import { defineStore } from 'pinia'
import { ref, computed } from 'vue';
import { ktlapi, ktluser, dateFormat } from '@/utils';

export interface IActiveItem {
	ID?: number;
	UUID?: string;
	Status?: number;
	StatusTitle?: string;
	RoomLink?: string;
	RoomNo?: string;
	RoomPwd?: string;
}

// 组合式 store
export const useClassStore = defineStore('class', () => {
	
	const LessonType = ref([{
			"value": 1,
			"label": "正式课"
		},
		{
			"value": 2,
			"label": "保温课"
		}
	])
	const SubjectList = ref([]);
	
	// 筛选类型1：0-全部，1-正课，2-保温
	const searchType = ref<number>(0);
	// 筛选类型2：1-已约课，2-已结束，3-已取消，4-已评价
	const subSearchType = ref(0);
	const minDate = ref<string>("");
	const maxDate = ref<string>("");
	const activeItem = ref<IActiveItem>({});
	const LessonList = ref<any[]>([]);
	const isTeacher = ref<boolean>(false);
	const isStudent = ref<boolean>(false);
	const Pop = ref(false);
	const CancelLes = ref(false);
	const roomConfirmDia = ref(false);
	
	const classListFilter = computed(() => {
		const tempFilter = LessonList.value.filter(x => {
			const condition1 = searchType.value == 0 || x.LessonType == searchType.value;
			const condition2 = subSearchType.value == 0 || x.Status == subSearchType.value;
			return condition1 && condition2;
		})
		
		if (isStudent.value){
			return tempFilter.map(item => {
				const startDate = dateFormat(`${item.StudyDay} ${item.StartTime}`, 'yyyy/MM/dd hh:mm:ss');;
				const endDate = dateFormat(`${item.StudyDay} ${item.EndTime}`, 'yyyy/MM/dd hh:mm:ss');
				const nowDate = dateFormat(item.NowDate, 'yyyy/MM/dd hh:mm:ss');
				item.isTimeOut = new Date(nowDate) < new Date(startDate) || new Date(nowDate) > new Date(endDate);
				
				return item;
			})
		}
		
		if (isTeacher.value) {
			return tempFilter;
		}
	})
	
	const setPop = (inPop: boolean) => Pop.value = inPop;
	const setCancelLes = (inCancelLes: boolean) => CancelLes.value = inCancelLes;
	const setRoomConfirmDia = (inRoomConfirmDia: boolean) => roomConfirmDia.value = inRoomConfirmDia;
	const setSubSearchType = (inSubSearchType: number) => subSearchType.value = inSubSearchType;
	const setSearchType = (inSearchType: number) => searchType.value = inSearchType;
	const setActiveItem = (inActiveItem: IActiveItem) => activeItem.value = inActiveItem;
	const setLessonList = (inLessonList: any[]) => LessonList.value = inLessonList;
	
	function handleToClassReport(data: any) {
		uni.navigateTo({
			url: `/pages-study/study/modules/step-final-result?LessonId=${data.UUID}&Type=view&isreview=${data.LessonType==1?'0':'1'}`
		})
	}
	
	function loadLessons() {
		uni.showLoading({ title: '加载中' });
		ktlapi({
			action: "Cus_MyLessonList",
			data: {
				"StudentAccountId": ktluser.BaseInfo().AccountID
			}
		}).then((res: any) => {
			LessonList.value = res.data;
			uni.hideLoading();
		});
	}
	
	function loadSubjectList() {
		uni.showLoading({ title: '加载中' });
		ktlapi({
			action: "Cus_GetStudentSubjecList",
			data: {
				"StudentAccountId": ktluser.BaseInfo().AccountID
			}
		}).then((res: any) => {
			SubjectList.value = res.data;
			uni.hideLoading();
		});
	}
	
	function initStudentData(){
		isStudent.value = true;
		isTeacher.value = false;
		
		var now = new Date();
		minDate.value = dateFormat(now) as string;
		maxDate.value = dateFormat(now.setMonth(now.getMonth() + 1)) as string;
		console.log(maxDate.value);
	}
	
	function initTeacherData(){
		isTeacher.value = true;
		isStudent.value = false;
	}
	
	return {
		classListFilter,
		searchType,
		subSearchType,
		roomConfirmDia,
		isTeacher,
		isStudent,
		Pop,
		CancelLes,
		minDate,
		maxDate,
		LessonType,
		SubjectList,
		LessonList,
		activeItem,
		initStudentData,
		initTeacherData,
		loadLessons,
		loadSubjectList,
		setPop,
		setActiveItem,
		setSearchType,
		setSubSearchType,
		setCancelLes,
		setRoomConfirmDia,
		setLessonList,
		handleToClassReport
	}
})

